<?php


namespace api\controllers;


use common\core\actions\GetAction;
use common\core\actions\PostAction;
use server\LogInterface;
use yii\base\Module;

class LogController extends Controller
{
    protected $log;

    public function __construct($id, Module $module, LogInterface $log, array $config = [])
    {
        $this->log = $log;
        parent::__construct($id, $module, $config);
    }

    public function actions()
    {
        return [
            'login' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'startTime',
                    'endTime'
                ],
                'data' => function ($data) {
                    //return $this->log->logList([]);
                    return $this->getList($this->log->loginLog($data));
                },
            ],
            'operation' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'startTime',
                    'endTime'
                ],
                'data' => function ($data) {
                    return $this->getList($this->log->operationLog($data));
                },
            ],
            'query-log' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'ip',
                    'module',
                    'startTime',
                    'endTime'
                ],
                'data' => function ($data) {
                    return $this->getList($this->log->queryLog($data));
                },
            ],
            'query-runtime' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'ip',
                    'startTime',
                    'endTime',
                ],
                'data' => function ($data) {
                    return $this->getList($this->log->queryRuntime($data));
                },
            ],
            'telnet-log' => [
                'class' => PostAction::class,
                'validateFiled' => [
                    'ip',
                ],
                'data' => function ($data) {
                    $data['user_id'] = \Yii::$app->user->identity->id;
                    $data['username'] = \Yii::$app->user->identity->username;
                    return $this->log->insertRemoteLoginLog($data);
                },
            ],
            'telnet' => [
                'class' => GetAction::class,
                'validateFiled' => [
                    'startTime',
                    'endTime'
                ],
                'data' => function ($data) {
                    return $this->getList($this->log->queryRemoteLoginLog($data));
                },
            ],
        ];
    }

}